<?php
/**
 * ADICIONA SISTEMA
 */
if(	isset($_POST['module']) && $_POST['module'] == 'supplement' && 
	isset($_POST['action']) && $_POST['action'] == 'add'){
	$name 			= $_POST['name'];
	$description 	= $_POST['description'];
	$system			= $_POST['system'];
	$supplement 	= new Supplement();
	
	$supplement->set_name($name);
	$supplement->set_description($description);
	$supplement->set_idSystem($system);
	$supplement->new_Supplement() or die("erro ao criar Suplemento");
}

class Supplement{

	private $idSupplement;
	private $name;
	private $description;
	private $system;
	
	function getSupplement($id){
		$q 					= "SELECT * FROM supplement WHERE idSupplement='$id'";
		$query 				= mysql_query("$q") or die(mysql_error());
		$query 				= mysql_fetch_array($query, MYSQL_BOTH);
		$this->idUser		= $query['idSupplement'];
		$this->name			= $query['name'];
		$this->description	= $query['description'];
		$this->system		= $query['idSystem'];
		return true;
	}
	
	/*
	 *  Funções GET
	 */

	function get_idSupplement(){	return $this->idSupplement;}
	function get_name(){			return $this->name;}
	function get_description(){		return $this->description;}
	function get_System(){			return $this->system;}		
	
	function set_idSupplement($id){
		$this->idSupplement = $id;
		return true;
	}//Insere o Id do Usuário
	function set_name($name){
		$this->name = $name;
		return true;
	}//Insere o Nome do Usuário
	function set_description($description){
		$this->description = $description;
		return true;
	}//Insere a Senha do Usuário
	
	function set_idSystem($system){
		$this->system = $system;
		return true;
	}//Insere A Data de Registro do Usuário
	
	function save_Supplement(){
		$q = 	"UPDATE `rpgcom`.`supplement` 
				SET		`name` 				= '$this->name',
						`description`		= '$this->description', 
						`idSystem`			= '$this->system'
				WHERE 	user.idSupplement 	= '$this->idSupplement';";
		$query = mysql_query("$q") or die(mysql_error());
		if($query)return true;else return false;
	}//Salva Alterações no Perfil
	
	function new_Supplement(){
		$q = "INSERT INTO supplement(name,
		                       description,
		                       idSystem)
		                VALUES( '$this->name',
		                		'$this->description',
		                		'$this->system';";
		$query = mysql_query("$q") or die(mysql_error());
		$q = "SELECT idSupplement FROM supplement WHERE name = '$this->name';";
		$query = mysql_query("$q") or die(mysql_error());
		$query = mysql_fetch_array($query, MYSQL_BOTH);
		if($query)return $query['idSupplement'];else return false;
	}//Registra novo usuario
	
	function buildSelectSupplement(){
		$query = "SELECT supplement.name, supplement.idSupplement, system.idSystem, system.name as sys  FROM supplement,system WHERE system.idSystem = supplement.idSystem";
		$result = mysql_query($query) or die(mysql_error());
		$numRows = mysql_num_rows($result);
		$cont = 0;
		$arr_table_result=mysql_fetch_full_result_array($result);
		if($numRows > 0){
			echo"<select name='supplement'>";
			while($cont<$numRows){
				$supplement 		= 	$arr_table_result[$cont]['name'];
				$id 				= 	$arr_table_result[$cont]['idSupplement'];
				$system 			= 	$arr_table_result[$cont]['sys'];
				echo"<option value='$id'>$supplement - $system</option>";
			$cont++;
			}
			echo"</select>";
		}else{echo "n&atilde;o existem suplementos cadastrados.";}
	}
	
}
?>